package at.guigu.controller;

import at.guigu.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * SpringMVC回写数据
 * 测试：@RestController注解
 */
//将Usercontroller放到Spring容器中

// @RestController = @Controller + @ResponseBody
@RestController
@RequestMapping("/userThree")
public class UserControllerThree {
    /**
     * SpringMVC回写数据——直接返回字符串方式二：直接返回普通字符串数据
     * @return
     */
    @RequestMapping(value = "/quick2")
    public String save2(){
        return "Hello World!";
    }

    /**
     * SpringMVC回写数据——直接返回字符串方式二：返回JSON格式响应字符串数据
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/quick3")
    public String save3() throws JsonProcessingException {
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        //使用JSON的转换工具将对象转换为JSON格式的字符串，然后在返回
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        return json;
    }
}
